$.fn.extend({
    scrollValue: function (option) {
        if (!option) {
            console.warning('未设置配置项或者不正确，若不使用，请不要调用,目前只进行了初始化');
        }
        let t = Object;
        let $t = this;
        //预设配置
        t.option = {
            preValue: 0,     //之前的数值
            value: 0,        //显示数值
            left: '',        //左侧标签
            right: '',       //右侧标签
            fixed: 0,        //保留小数点位数
            color: '#000',   //显示颜色
            max: 20,         //数值最大位数（不包含小数点后)
        };
        t.length = 0;
        //载入配置信息
        t.initOption = function () {
            //判断是否已经初始化
            //未初始化
            if (!$t.data('inited')) {
                //实例配置
                if (typeof option == 'object') {  //配置信息为对象
                    if (option.value != 'undefined' && (typeof option.value != 'number' || option.value - 0 !== option.value - 0)) {
                        console.warning('value必须为数值')
                        return false;
                    }
                    if (option.left != 'undefined' && typeof option.left != 'string') {
                        console.warning('left必须为字符串')
                        return false;
                    }
                    if (option.right != 'undefined' && typeof option.right != 'string') {
                        console.warning('right必须为字符串')
                        return false;
                    }
                    if (option.fixed != 'undefined' && (typeof option.fixed != 'number' || option.fixed - 0 !== option.fixed - 0)) {
                        console.warning('fixed必须为数值')
                        return false;
                    }
                    if (option.color != 'undefined' && typeof option.color != 'string') {
                        console.warning('color必须为字符串')
                        return false;
                    }
                    t.option = Object.assign({}, t.option, option);
                } else if (typeof option == 'number') {  //配置信息为数字
                    t.option.value = option;
                } else {
                    console.warning('未设置配置项或者不正确，请查阅后使用')
                    return false;
                }
                let _option = t.option,
                    _left = _option.left,
                    _right = _option.right,
                    _fixed = _option.fixed,
                    _value = (_option.value - 0).toFixed(_fixed),
                    _preValue = (_option.preValue - 0).toFixed(_fixed);
                _option.value = _value;
                _option.preValue = _preValue;
                let valueArr = _value.split('.');
                let preValueArr = _preValue.split('.');
                if (valueArr.length > 1) {
                    let r = valueArr[0].split('')
                    t.valueArr = [];
                    t.valueArr.push(r);
                    t.valueArr.push(valueArr[1].split(''));
                    r = preValueArr[0].split('');
                    t.preValueArr = [];
                    t.preValueArr.push(r);
                    t.preValueArr.push(preValueArr[1].split(''))
                } else {
                    t.valueArr = [].push(valueArr[0].split(''))
                    t.preValueArr = [].push(preValueArr[0].split(''))
                }
                $t.data('inited', true)
                t.addAttrData();
                t.init();
            }
            //已初始化，则更新
            else {
                t.option.left = $t.data('left') || t.option.left;
                t.option.right = $t.data('right') || t.option.right;
                t.option.fixed = $t.data('fixed') || t.option.fixed;
                t.option.color = $t.data('color') || t.option.color;
                t.option.preValue = $t.data('value') || t.option.preValue;
                if (typeof option == 'object') {  //配置信息为对象
                    if (option.value != 'undefined' && (typeof option.value != 'number' || option.value - 0 !== option.value - 0)) {
                        console.warning('value必须为数值')
                        return false;
                    }
                    if (option.left != 'undefined' && typeof option.left != 'string') {
                        console.warning('left必须为字符串')
                        return false;
                    }
                    if (option.right != 'undefined' && typeof option.right != 'string') {
                        console.warning('right必须为字符串')
                        return false;
                    }
                    if (option.fixed != 'undefined' && (typeof option.fixed != 'number' || option.fixed - 0 !== option.fixed - 0)) {
                        console.warning('fixed必须为数值')
                        return false;
                    }
                    if (option.color != 'undefined' && typeof option.color != 'string') {
                        console.warning('color必须为字符串')
                        return false;
                    }
                    t.option = Object.assign({}, t.option, option);
                } else if (typeof option == 'number') {  //配置信息为数字
                    t.option.value = option;
                } else {
                    console.warning('未设置配置项或者不正确，请查阅后使用')
                    return false;
                }
                t.option.value = (t.option.value - 0).toFixed(t.option.fixed);
                t.option.preValue = (t.option.preValue - 0).toFixed(t.option.fixed)
                let valueArr = t.option.value.split('.');
                let preValueArr = t.option.preValue.split('.');
                if (valueArr.length > 1) {
                    let r = valueArr[0].split('')
                    t.valueArr = [];
                    t.valueArr.push(r);
                    t.valueArr.push(valueArr[1].split(''));
                    r = preValueArr[0].split('');
                    t.preValueArr = [];
                    t.preValueArr.push(r);
                    t.preValueArr.push(preValueArr[1].split(''))
                } else {
                    t.valueArr = [].push(valueArr[0].split(''))
                    t.preValueArr = [].push(preValueArr[0].split(''))
                }
                t.addAttrData();
                t.update()
            }
        }
        //init 初始化
        t.init = function () {
            let $dom = t.createBox();
            $t.append($dom);
            t.generateValue();
        }
        //更新数据
        t.update = function () {
            t.generateValue();
            $t.find('.left').html(t.option.left)
            $t.find('.right').html(t.option.right)
        }
        /*
        ** 生成单个数值
        ** @params boxId 数值框的类
        ** @params num 数值
        */
        t.createSingleNum = function (boxId, num) {
            return '<span class="sv_number ' + boxId + '-' + num + '">' + num + '</span>'
        }
        /*
        ** 生成单个数值框
        ** @params boxId 数值框的类
        */
        t.createSingleBox = function (boxId, animateID) {
            let boxStr = '<div class="sv_box box-' + boxId + ' ' + (animateID || '') + '">';
            boxStr += '<div class="toscroll" style="transform: rotateX(0deg)">'
            for (let i = 0; i < 10; i++) {
                boxStr += t.createSingleNum('number', i)
            }
            boxStr += '</div></div>';
            return boxStr;
        }
        /*
        ** 生成整个框
        */
        t.createBox = function () {
            let max = t.option.max;
            let total = '<div class="sv" id="sv" style="color: ' + t.option.color + '">';
            total += '<span class="sv_left">' + t.option.left + '</span>';
            for (let i = max; i >= 1; i--) {
                if (i != 1)
                    total += t.createSingleBox(i, 'toadd');
                else
                    total += t.createSingleBox(i);
            }
            if (t.option.fixed > 0) {
                total += '<span class="dot" style="background-color:' + t.option.color + '"></span>'
            }
            for (let i = 1; i <= t.option.fixed; i++) {
                total += t.createSingleBox('fixed-' + i);
            }
            total += '<span class="sv_right">' + t.option.right + '</span>';
            total += '</div>'
            return total;
        }

        t.generateValue = function () {
            let _vr = t.valueArr,  //当前数值小数点的划分数组
                _pvr = t.preValueArr; //之前数值小数点的划分数组
            let preDotBeforeLen = _pvr[0].length,
                preDotAfterLen = _pvr[1].length || 0;
            let preTotalVr = [].concat(_pvr[0], (_pvr[1] || [])),
                preTotalValue = preTotalVr.join('');
            let dotBeforeLen = _vr[0].length,
                dotAfterLen = _vr[1].length || 0;
            let totalVr = [].concat(_vr[0], (_vr[1] || [])),
                totalValue = totalVr.join('');
            let showDeg, actualDeg;
            let i = 0;
            while (dotAfterLen + dotBeforeLen > 0) {
                let dv = totalValue - preTotalValue;
                let currentDeg = dv * 36;
                let preDeg = String;
                if (dotAfterLen > 0) {
                    i = dotAfterLen--;
                    preDeg = t.getPreDeg(i, 'after');
                    showDeg = currentDeg + preDeg;
                    actualDeg = showDeg % 360;
                    $('.box-fixed-' + i + ' .toscroll')
                        .addClass('tochange')
                    !function (_i, _showDeg) {
                        setTimeout(() => {
                            $('.box-fixed-' + _i + ' .toscroll')
                                .css({
                                    'transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-webkit-transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-moz-transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-o-transform': 'rotateX(' + _showDeg + 'deg)',
                                })
                        }, 1)
                    }(i, showDeg)
                    !function (_i) {
                        setTimeout(() => {
                            $('.box-fixed-' + _i + ' .toscroll')
                                .removeClass('tochange')
                        }, 1501)
                    }(i)
                    !function (_i, _actualDeg) {
                        setTimeout(() => {
                            $('.box-fixed-' + _i + ' .toscroll')
                                .css({
                                    'transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-webkit-transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-moz-transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-o-transform': 'rotateX(' + _actualDeg + 'deg)',
                                })
                        }, 1502)
                    }(i, actualDeg)
                    preTotalVr.pop();
                    preTotalValue = preTotalVr.join('');
                    totalVr.pop();
                    totalValue = totalVr.join('');
                } else {
                    preDeg = t.getPreDeg(i);
                    showDeg = currentDeg + preDeg;
                    actualDeg = showDeg % 360;
                    !function (_i) {
                        setTimeout(() => {
                            $('.box-' + _i)
                                .removeClass('toadd')
                                .find('.toscroll')
                                .addClass('tochange')
                        }, 1)
                    }(i)
                    !function (_i, _showDeg) {
                        setTimeout(() => {
                            $('.box-' + _i + ' .toscroll')
                                .css({
                                    'transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-webkit-transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-moz-transform': 'rotateX(' + _showDeg + 'deg)',
                                    '-o-transform': 'rotateX(' + _showDeg + 'deg)',
                                })
                        }, 2)
                    }(i, showDeg)
                    !function (_i) {
                        setTimeout(() => {
                            $('.box-' + _i + ' .toscroll')
                                .removeClass('tochange')
                        }, 1502)
                    }(i)
                    !function (_i, _actualDeg) {
                        setTimeout(() => {
                            $('.box-' + _i + ' .toscroll')
                                .css({
                                    'transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-webkit-transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-moz-transform': 'rotateX(' + _actualDeg + 'deg)',
                                    '-o-transform': 'rotateX(' + _actualDeg + 'deg)',
                                })
                        }, 1503)
                    }(i, actualDeg)
                    i++;
                    dotBeforeLen--;
                    preTotalVr.pop();
                    preTotalValue = preTotalVr.join('');
                    totalVr.pop();
                    totalValue = totalVr.join('');
                }
            }
            for (let _i = i; _i <= t.option.max; _i++) {
                $('.box-' + _i)
                    .addClass('toadd')
                    .find('.toscroll')
                    .css('transform', 'rotateX(0deg)')
            }
        }

        t.getPreDeg = function (index, direction) {
            if (direction == 'after') {  //小数点后的判断
                return $('.box-fixed-' + index + ' .toscroll')[0].style.transform.replace(/(rotateX\()|(deg\))/g, '') - 0 || 0;
            } else {
                return $('.box-' + index + ' .toscroll')[0].style.transform.replace(/(rotateX\()|(deg\))/g, '') - 0 || 0;
            }
        }
        /*
        ** 保存消息
        */
        t.addAttrData = function () {
            $t.data(t.option)
        }
        t.initOption();

    }
})